package com.zsinfo.guoss.supplier.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.NewsBean;
import com.example.netcenter.bean.ResultsList;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lixl on 2018/12/4.
 * <p>
 * desc:消息中心
 */
public class NewsActivity extends BaseActivity {

    private XRecyclerView xRecyclerView;

    private TextView tv_msgTitle, tv_msgContent;
    private ImageView iv_close;
    private PopupWindow mPopupWindow;

    private CommonAdapter newsAdapter;
    private ResultsList<NewsBean> resultsLists;  //服务器返回的数据
    private List<NewsBean> list = new ArrayList<>();  //列表数据
    private int pageNo = 1;
    private int pageSize = 10;
    private boolean isLast = false; //是否是最后一条数据
    private boolean isRefresh = false, isLoadMore = false; //默认不刷新，不加载
    
    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_news;
    }

    @Override
    protected void initView() {
        setTitle("消息");
        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initPopWindow();
        setAdapter();
    }

    @Override
    protected void initNetData() {
        GetNetCenter().GetNetCenter(this,
                RetrofitBuilder.CreateApi().MSG_LIST(
                        ConstantsCode.supplier_msg_list,
                        SharedPreferencesUtil.getID(),
                        pageNo + "", pageSize + ""), new Response() {
                    @Override
                    public void OK(Object data) {
                        Log.e("lixl", data.toString());
                        setDataView(data);
                    }

                    @Override
                    public void Fail(Throwable e) {
                        Log.e("lixl", e.getMessage());
                    }

                    @Override
                    public void complete() {
                        if (isRefresh) {
                            xRecyclerView.refreshComplete();
                        }
                        if (isLoadMore) {
                            xRecyclerView.loadMoreComplete();
                        }
                    }
                });
    }

    @Override
    protected void customDestroy() {

    }

    /**
     * 初始化popWindow
     */
    private void initPopWindow() {
        View view = View.inflate(this, R.layout.window_pushmsg, null);
        tv_msgTitle = (TextView) view.findViewById(R.id.msgTitle);
        tv_msgContent = (TextView) view.findViewById(R.id.msgContent);
        iv_close = (ImageView) view.findViewById(R.id.close_pop);
        if (mPopupWindow == null) {
            mPopupWindow = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        }
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                newsAdapter.notifyDataSetChanged();
            }
        });
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPopupWindow != null && mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                }
            }
        });
        mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_transparent));
    }

    private void setAdapter() {
        xRecyclerView = findViewById(R.id.recyclerview);
        xRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        xRecyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //清除数据
                list.clear();
                //更新适配器
                newsAdapter.notifyDataSetChanged();
                //刷新操作
                pageNo = 1;
                //重新请求接口
                initNetData();
                //正在执行刷新
                isRefresh = true;
            }

            @Override
            public void onLoadMore() {
                //可以判断 isLast=true，表示已经没有更多数据了
                if (isLast) {
                    showToast("当前已经没有更多数据了");
                    xRecyclerView.loadMoreComplete(); //停止加载的动画效果
                    return;
                }
                //加载更多
                pageNo++;
                //请求加载接口
                initNetData();
                //正在执行加载
                isLoadMore = true;
            }
        });
        newsAdapter = new CommonAdapter<NewsBean>(this, R.layout.item_news, list) {
            @Override
            protected void convert(ViewHolder holder, final NewsBean newsBean, int position) {
                //数据展示
                holder.setText(R.id.tv_newscontent, newsBean.getContent());
                holder.setText(R.id.tv_news_time, newsBean.getCreateTime());
                //判断审核状态：0审核中，1审核成功，-1审核失败
                int type = newsBean.getType();
                if (type == 1) {
                    holder.setText(R.id.tv_news_type, "竞价成功")
                            .setTextColorRes(R.id.tv_news_type, R.color.black);
                } else if (type == 2) {
                    holder.setText(R.id.tv_news_type, "竞价失败")
                            .setTextColorRes(R.id.tv_news_type, R.color.red);
                } else if (type == 3) {
                    holder.setText(R.id.tv_news_type, "上新成功")
                            .setTextColorRes(R.id.tv_news_type, R.color.black);
                }  else if (type == 4) {
                    holder.setText(R.id.tv_news_type, "上新失败")
                            .setTextColorRes(R.id.tv_news_type, R.color.red);
                }
            }
        };
        newsAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                position--; //防止数组下标越界
                showPop(position);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        xRecyclerView.setAdapter(newsAdapter);
    }

    /**
     * 显示Popwindow
     *
     * @param position 显示的位置
     */
    private void showPop(final int position) {
        tv_msgTitle.setText(list.get(position).getTitle());
        tv_msgContent.setText(list.get(position).getContent());
        mPopupWindow.showAtLocation(xRecyclerView, Gravity.CENTER, 0, 0);
    }


    /**
     * 设置数据到view
     *
     * @param data
     */
    private void setDataView(Object data) {
        resultsLists = (ResultsList<NewsBean>) data;
        //赋值
        isLast = resultsLists.getData().isIsLast();
        //判断一下，currentPageNo==1是表示第一次加载数据，如果第一次加载数据为空，就表示没有数据
        List<NewsBean> datas = resultsLists.getData().getObjects();
        if (pageNo == 1) {
            if (datas == null || datas.size() == 0) {
                SetNoData(true);
                return;
            } else {
                SetNoData(false);
            }
        }
        //更新适配器
        list.addAll(datas);
        newsAdapter.notifyDataSetChanged();
    }
}
